package com.example.demo.controller;

import com.example.demo.config.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @program: demo
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2022-04-20 15:42
 **/
@RestController()
public class KongTestController {

    @GetMapping("/redis/getV")
    public Map<String,String> redis(String k) throws InterruptedException {
//        Thread.sleep(10000);
        Map<String,String> map = new HashMap<>();
        map.put("message","GetMapping OK ... "+k);

        synchronized (KongTestController.class){

            System.out.println("lock1");

            Thread.sleep(3000L);
            synchronized (RedisTestController.class){

                System.out.println("lock2");





            }


        }

        return map;
    }

    @GetMapping("/redis/getV1")
    public Map<String,String> redis2(String k) throws InterruptedException {
//        Thread.sleep(10000);
        Map<String,String> map = new HashMap<>();
        map.put("message","GetMapping OK ... "+k);

        synchronized (RedisTestController.class){

            System.out.println("lock2");

            Thread.sleep(3000L);
            synchronized (KongTestController.class){

                System.out.println("lock1");





            }


        }

        return map;
    }

    @GetMapping("/demo/redis")
    public Map<String,String> getV() throws InterruptedException {
//        Thread.sleep(10000);
        Map<String,String> map = new HashMap<>();
        map.put("message","GetMapping OK ...demo ");
        return map;
    }

    @GetMapping("/redis")
    public Map<String,String> getV2() throws InterruptedException {
//        Thread.sleep(10000);
        Map<String,String> map = new HashMap<>();
        map.put("message","GetMapping OK ...redis ");
        return map;
    }

    @GetMapping("/test/sleep")
    public Map<String,String> sleep(@RequestParam("s") Integer s) throws InterruptedException {
        Thread.sleep(s);
//        Thread.sleep(10000);
        Map<String,String> map = new HashMap<>();
        map.put("message","GetMapping OK ... "+s);
        return map;
    }


    @GetMapping("/kong-test/kong-test/get")
    public Map<String,String> get() throws InterruptedException {
//        Thread.sleep(10000);
        Map<String,String> map = new HashMap<>();
        map.put("message","GetMapping OK");
        return map;
    }


    @PostMapping("/kong-test/kong-test/post")
    public Map<String,String> post(@RequestBody Map<String,String> map){
        if(CollectionUtils.isEmpty(map)){
            map = new HashMap<>();
            map.put("message","PostMapping OK");
        }
        map.put("message","PostMapping OK");
        return map;
    }

    @PostMapping("/kong-test/kong-test/post2")
    public Map<String,String> post2(@RequestBody Map<String,String> map){
        if(CollectionUtils.isEmpty(map)){
            map = new HashMap<>();
            map.put("message","PostMapping OK");
        }
        map.put("message","PostMapping OK");
        return map;
    }


    @PatchMapping("/kong-test/kong-test/patch")
    public Map<String,String> patch(){
        Map<String,String> map = new HashMap<>();
        map.put("message","PatchMapping OK");
        return map;
    }

    @PutMapping("/kong-test/kong-test/put")
    public Map<String,String> put(){
        Map<String,String> map = new HashMap<>();
        map.put("message","PutMapping OK");
        return map;
    }

    @DeleteMapping("/kong-test/kong-test/delete")
    public Map<String,String> delete(){
        Map<String,String> map = new HashMap<>();
        map.put("message","DeleteMapping OK");
        return map;
    }

    @RequestMapping(value = "/kong-test/kong-test/options",method = RequestMethod.OPTIONS)
    public Map<String,String> options(){
        Map<String,String> map = new HashMap<>();
        map.put("message","OPTIONSMapping OK");
        return map;
    }

    @RequestMapping(value = "/kong-test/kong-test/head",method = RequestMethod.HEAD)
    public Map<String,String> head(){
        new Date();
        Map<String,String> map = new HashMap<>();
        map.put("message","HEADMapping OK");
        return map;
    }

    public static void main(String[] args) {
        ClassPathResource classPathResource = new ClassPathResource("redis/skill.lua");
        System.out.println(classPathResource.getPath());

    }

}
